#include "bqddjs.h"
#include "ui_bqddjs.h"
#include<QDebug>
#include <QScreen>
#include <cmath>
#include <QString>
#include <t_value.h>
#include<physicaltools.h>

int k;
double mean, sum, stdev, b_uncertainty, a_uncertainty, final_uncertainty, tvalue;

bqddjs::bqddjs(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::bqddjs)
{
    ui->setupUi(this);
    k = ui->datanum->text().toInt();

    //获取设备屏幕大小
    QRect screenRect = QGuiApplication::primaryScreen()->geometry();
    //获取设备像素比
    int screenW = screenRect.width();
    int screenH = screenRect.height();
    this->move ((screenW - this->width())/2,(screenH - this->height())/2); //窗口居中显示

    on_dataconfirm_clicked();
    on_datanum_returnPressed();
    on_pushButton_clicked(); //初始化防止点击多次显示结果的情况发生

    //on_datanum_editingFinished();
    setAttribute(Qt::WA_DeleteOnClose); //保证子窗口析构函数运行
}

bqddjs::~bqddjs()
{
    delete ui;

    //删除QList占用的内存及清空列表
    qDeleteAll(datalist);
    datalist.clear();
    qDeleteAll(datanamelist);
    datanamelist.clear();
}





void bqddjs::on_dataconfirm_clicked()
{
    //保证数据表为空
    qDeleteAll(datalist);
    datalist.clear();
    qDeleteAll(datanamelist);
    datanamelist.clear();
    //获得数据数目
    k = ui->datanum->text().toInt();
    //连接动态创建输入框函数
    connect(ui->dataconfirm,&QPushButton::clicked,this,&bqddjs::createtest); //连接createtest函数动态创建文本框
}

//数据框创建函数
void bqddjs::createtest()
{
    for(int i=0;i<k;i++)
    {
        srk=new QLineEdit(this);
        //设置输入框在主对话框的位置
        srk->setGeometry(100,200+28*i,75,25);
        //设置数据对象名字
        srk->setObjectName("data"+QString::number(i));
        //向QList添加成员项
        datalist.append(srk);
        //关联信号槽函数
        //connect(datalist.at(i),SIGNAL(clicked()),this,SLOT(slotButtonClicked()));
        srk->show();
        //qDebug() << ui->data1->text().toInt();
        dataname=new QLabel(this);
        //设置输入框在主对话框的位置
        dataname->setGeometry(50,200+28*i,50,25);
        //设置数据对象名字
        dataname->setText("数据"+QString::number(i+1));
        datanamelist.append(dataname);
        dataname->show();
        //qDebug() << datalist.at(i)->text().toInt() << endl;
     }
   /* B_uncertainty_name=new QLabel(this);
    B_uncertainty_name->setGeometry(10,200+28*k,150,25);
    B_uncertainty_name->setText("请输入B类不确定度");
    datanamelist.append(B_uncertainty_name);
    B_uncertainty_name->show();*/



}

void bqddjs::on_datanum_returnPressed()
{
    qDeleteAll(datalist);
    datalist.clear();
    qDeleteAll(datanamelist);
    datanamelist.clear();

    k = ui->datanum->text().toInt();
    connect(ui->datanum,&QLineEdit::returnPressed,this,&bqddjs::createtest);
    //连接createtest函数动态创建文本框
}

void bqddjs::bqddcalculate() //计算不确定度函数
{
    tvalue = ui->t_value->text().toDouble();
    b_uncertainty = ui->lineEdit->text().toDouble();

    double *p = new double[k];
    sum = 0;
    for(int i = 0;i < k;i ++){
        p[i] = datalist.at(i)->text().toDouble();
        sum += p[i];
    }
    mean = sum/k; //平均值
    double stdev_sum = 0;
    for(int i = 0;i < k;i ++){
        stdev_sum += pow(p[i]-mean, 2);
    }
    stdev = sqrt(stdev_sum/(k-1)); //计算标准差
    a_uncertainty = tvalue * stdev / sqrt(k); //计算a类不确定度
    final_uncertainty = sqrt(pow(a_uncertainty, 2) + pow(b_uncertainty, 2));  //计算合成不确定度
    qDebug() << final_uncertainty << Qt::endl;
    //显示结果
    ui->average->setText(QString("%1").arg(mean));
    ui->stdev->setText(QString("%1").arg(stdev));
    ui->a_value->setText(QString("%1").arg(a_uncertainty));
    ui->final_value->setText(QString("%1").arg(final_uncertainty));
    ui->stdev->show();
    ui->a_value->show();
    ui->final_value->show();
    ui->average->show();
    delete [] p;

}



void bqddjs::on_checkt_clicked() //查看t值
{
    //跳转到子窗口
    t_value *fater = new t_value();
    this->close();
    fater->show();
}

void bqddjs::on_back_clicked() //转到菜单栏
{
    physicaltools *fater = new physicaltools();
    this->close();
    fater->show();
}

void bqddjs::on_pushButton_clicked()
{
    connect(ui->pushButton,&QPushButton::clicked,this,&bqddjs::bqddcalculate); //连接计算函数
}

void bqddjs::on_refresh_clicked()
{
    bqddjs *fater = new bqddjs();
    this->close();
    fater->show();
}

void bqddjs::on_import_2_clicked()//导入数据
{
    qDeleteAll(datalist);
    datalist.clear();
    qDeleteAll(datanamelist);
    datanamelist.clear();

    QString data_get;
    bool ok;
    data_get = QInputDialog::getText(this, tr("请输入你的数据"),tr("请输入数据"), QLineEdit::Normal,0, &ok);
    QStringList strListx = data_get.split("	");
    QStringList strListy = data_get.split("\n");//分离数据
    QStringList strList;

    //判断数据格式
    bool ify=0;
    if(strListx.size()==1 && strListy.size()!=2) {
        strList = strListy;
        ify = 1;
    }
    else if(strListy.size()==2 && strListx.size()!=1) strList = strListx;

    else QMessageBox::critical(this, tr("危险弹窗"),  tr("您输入的数据错误，请重新输入或手动输入"));
    k=strList.size();
    if(ify) k--;
    for (int i = 0; i < k; i++) {
        qDebug()<<strList[i] << Qt::endl;        //设置数据
    }

    qDebug() << k;
    createtest();

    for (int i = 0; i < k; i++) {
        datalist.at(i)->setText(strList[i]);        //设置数据
    }
   }

